<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('get_javascript_basic_lang'))
{
	function get_javascript_basic_lang()
	{
		$CI =& get_instance();
		$javascriptArray = "var lang={};";
		$actual_lang = $CI->config->item('language');
		$lang_key = array_search($actual_lang, $CI->config->item('lang_uri_abbr'));
		$javascriptArray.="lang.lang_key='{$lang_key}';";
		$javascriptArray.="lang.lang='{$actual_lang}';";
		return $javascriptArray;
	}
}

if ( ! function_exists('get_javascript_lang'))
{
	function get_javascript_lang()
	{
		$CI =& get_instance();
		$javascriptArray = "var lang = {};";
                
		foreach ($CI->lang->language as $key=>$val){
			if (strip_tags($val,'<p>') == $val) {
				$key = str_replace(array('&','-'), array('and','_'), $key);
				
				$javascriptArray.= "lang." . trim($key) . "='" . addslashes(strip_tags($val)) . "';";
			}
		}
		$actual_lang = $CI->config->item('language');

		$javascriptArray.="lang.lang_key = '{$CI->lang->lang()}';";
		$javascriptArray.="lang.lang = '{$actual_lang}';";
		return $javascriptArray;
	}
}

 function lang($line, $html = false, $id = '')
 {
 	$langKey = $line;
	$CI =& get_instance();
	$line = $CI->lang->line($line);

	$args = func_get_args();

	if(is_array($args)) array_shift($args);

	if(is_array($args) && count($args))
	{
	  foreach($args as $arg)
	  {
		  $line = str_replace_first('%s', $arg, $line);
	  }
	}
	
	if (empty($line)) {
		$line = '[' . $langKey . ']';
	}
	if ($id != '')
	{
		$line = '<label for="'.$id.'">'.$line."</label>";
	}

	if (!$html) {
		return strip_tags ($line);
	}
	else {
		return $line;
	}
 }

 function str_replace_first($search_for, $replace_with, $in)
 {
     $pos = strpos($in, $search_for);
     if($pos === false)
     {
         return $in;
     }
     else
     {
         return substr($in, 0, $pos) . $replace_with . substr($in, $pos + strlen($search_for), strlen($in));
     }
 }